﻿#nullable enable

using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PHMEE.Controllers;

namespace PHMEE.EnergyEfficiency;

/// <summary>
/// 报表导出
/// </summary>
[Route("api/cbm/report")]
public class ReportController : PHMEEController
{
    private readonly IReportAppService _reportAppService;
    private readonly IFlowmeterExportAppService _exportAppService;

    public ReportController(IReportAppService reportAppService, IFlowmeterExportAppService exportAppService)
    {
        _reportAppService = reportAppService;
        _exportAppService = exportAppService;
    }

    /// <summary>
    /// 导出 IMO_DCS 报表
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDate"></param>
    /// <param name="endDate"></param>
    /// <returns></returns>
    [Route("ExportIMODCSReport")]
    [HttpGet]
    public async Task<FileResult> ExportIMODCSReportAsync(Guid vesselId, [Required] DateTime startDate,
        [Required] DateTime endDate)
    {
        var utcNow = DateTime.UtcNow;
        var stream = await _reportAppService.ExportIMODCSReportAsync(vesselId, startDate, endDate);
        return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"IMO_DCS_{utcNow:yyyyMMdd}.xlsx");
    }

    /// <summary>
    /// 导出 MRV 报表
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDateTime"></param>
    /// <param name="endDateTime"></param>
    /// <returns></returns>
    [Route("ExportMRVEUReport")]
    [HttpGet]
    public async Task<FileResult> ExportMRVEUReportAsync(Guid vesselId, [Required] DateTime startDateTime, [Required] DateTime endDateTime)
    {
        var utcNow = DateTime.UtcNow;
        var stream = await _reportAppService.ExportMRVEUReportAsync(vesselId, startDateTime, endDateTime);
        return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"MRV_EU_{utcNow:yyyyMMdd}.xlsx");
    }

    /// <summary>
    /// 导出 LogAbstracts
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDateTime"></param>
    /// <param name="endDateTime"></param>
    /// <returns></returns>
    [Route("ExportLogAbstracts")]
    [HttpGet]
    public async Task<FileResult> ExportLogAbstractsAsync(Guid vesselId, [Required] DateTime startDateTime,
        [Required] DateTime endDateTime)
    {
        var utcNow = DateTime.UtcNow;
        var stream = await _reportAppService.ExportLogAbstractsAsync(vesselId, startDateTime, endDateTime);
        return File(stream, "text/csv", $"LogAbstracts_{utcNow:yyyyMMdd}.csv");
    }
    /// <summary>
    /// 日期范围内导出流量计历史数据
    /// </summary>
    /// <returns></returns>
    [Route("GenerateFlowmeterSheetsExcel")]
    [HttpGet]
    public async Task<IActionResult> GenerateFlowmeterSheetsExcel(Guid vesselId, [Required] DateTime startDate, [Required] DateTime endDate)
    {
        var bytes = await _exportAppService.GenerateFlowmeterSheetsExcelAsync(vesselId, startDate, endDate);
        string fileName = $"Historical data of flow meter({startDate:yyyyMMdd}).xlsx";
        if (startDate != endDate)
        {
            fileName = $"Historical data of flow meter({startDate:yyyyMMdd}-{endDate:yyyyMMdd}).xlsx";
        }
        return File(
            fileContents: bytes,
            contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            fileDownloadName: fileName);
    }
}